package com.yanqu.road.server.cmd.crossunion;

import com.yanqu.road.entity.activity.UnionActivityConditionData;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.StrongholdWarProto;
import com.yanqu.road.pb.cross.activity.CrossActivityProto;
import com.yanqu.road.pb.crossunion.activity.CrossUnionActivityRankDataProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.cmd.CrossUnionCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.union.CrossUnionActivityDataMgr;
import com.yanqu.road.server.manager.user.CrossUserMgr;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.utils.string.StringUtils;

@Cmd(code = CrossProtocol.C_CROSS_UNION_ACTIVITY_RANK_LIST, desc = "")
public class CrossUnionRankListCmd extends CrossCommand {
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        CrossUnionActivityRankDataProto.CrossUnionActivityDataMsg msg = CrossUnionActivityRankDataProto.CrossUnionActivityDataMsg.parseFrom(packet.getBytes());
        int activityId = msg.getActivityId();
        long userId = msg.getUserId();
        int conditionType = msg.getConditionType();
        String unionId = "";
        String localValue = "0";
        UnionActivityConditionData ucd = CrossUnionActivityDataMgr.getUnionActivityConditionData(activityId, conditionType, userId);
        if (null != ucd) {
            unionId = ucd.getUnionUid();
            localValue = ucd.getValue().toString();
        }
        //巅峰商会战虽然加入了商会，但是名单一开始就确定了，他不能白嫖没有他，但是他又要看榜单
        if(conditionType == eGamePlayerEventType.RankUnionWarUnionScoreRank.getValue() && StringUtils.isNullOrEmpty(unionId)){
            UserBaseInfo userBaseInfo = CrossUserMgr.getUserBaseInfo(userId);
            if(userBaseInfo != null){
                unionId = userBaseInfo.getUnionUid();
            }
        }
        int rankListType = msg.getRankListType();
        if (rankListType == 1) {
            CrossActivityProto.CrossUnionActivtyRankReqMsg.Builder req = CrossActivityProto.CrossUnionActivtyRankReqMsg.newBuilder();
            req.setActivtyId(activityId);
            req.setConditionType(0);
            req.setLocalValue(localValue);
            req.setUnionId(unionId);
            YanQuMessage message = YanQuMessageUtils.buildMessage(CrossProtocol.C2_CROSS_UNION_ACTIVITY_RANK, req);
            MessageHelper.sendPacket(serverId, userId, message);
        } else if (rankListType == 2) {
            StrongholdWarProto.CrossStrongholdActivityUnionRankListReqMsg.Builder req = StrongholdWarProto.CrossStrongholdActivityUnionRankListReqMsg.newBuilder();
            req.setActivityId(activityId);
            req.setConditionType(conditionType);
            req.setUnionUid(unionId);
            req.setUserId(userId);
            YanQuMessage message = YanQuMessageUtils.buildMessage(CrossProtocol.C_CROSS_GENERAL_UNION_RANK, req);
            MessageHelper.sendPacket(serverId, userId, message);
        } else if (rankListType == 4) {
            CrossActivityProto.CrossUnionActivtyRankReqMsg.Builder req = CrossActivityProto.CrossUnionActivtyRankReqMsg.newBuilder();
            req.setActivtyId(activityId);
            req.setConditionType(0);
            req.setLocalValue(localValue);
            req.setUnionId(unionId);
            YanQuMessage message = YanQuMessageUtils.buildMessage(CrossProtocol.C2_CROSS_GET_RELATION_ACTIVITY_UNION_RANK, req);
            MessageHelper.sendPacket(serverId, userId, message);
        } else if (rankListType == 5) {
            CrossActivityProto.CrossUnionActivtyRankReqMsg.Builder req = CrossActivityProto.CrossUnionActivtyRankReqMsg.newBuilder();
            req.setActivtyId(activityId);
            req.setConditionType(0);
            req.setLocalValue(localValue);
            req.setUnionId(unionId);
            YanQuMessage message = YanQuMessageUtils.buildMessage(CrossProtocol.C2_CROSS_GET_ORE_ACTIVITY_UNION_RANK, req);
            MessageHelper.sendPacket(serverId, userId, message);
        }
    }

    @Override
    public void sendData() {

    }
}
